﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;

namespace UIT.Controls.Template
{
    public class DataTemplateInstance : Control, IContainer, IContainable, IContainable<IContainer>
    {
        public DataTemplateInstance(object dataContext, IControl child)
        {
            UpdateDataContext(dataContext);
            children = new SingleControlCollection(this);
            Child = child;
        }

        public override object DataContext
        {
            get
            {
                return base.DataContext;
            }
            set
            {
            }
        }

        public void UpdateDataContext(object dataContext)
        {
            base.DataContext = dataContext;
        }

        IContainer IContainable<IContainer>.Parent
        {
            get
            {
                return ((IContainable)this).Parent;
            }
            set
            {
                ((IContainable)this).Parent = value;
            }
        }

        IContainer IContainable.Parent
        {
            get { return base.Parent; }
            set
            {
                if (parent != null)
                    ((IControlContainer)parent).Children.Remove(this);
                parent = value;
                if (parent != null)
                    ((IControlContainer)parent).Children.Add(this);
                OnPropertyChanged("Parent");
            }
        }

        public DataTemplateInstance(IControl control)
            : this(null, control)
        {

        }

        private SingleControlCollection children;

        public IControl Child
        {
            get { return children.Child; }
            set
            {
                children.Add(value);
                OnPropertyChanged("Child");
            }
        }

        public override bool IsContainer
        {
            get { return false; }
        }

        public override ControlType Type
        {
            get { return ControlType.Template; }
        }

        #region IContainer<IContainer,IControl> Members

        IDisposableObservableCollection<IContainer, IControl> IContainer<IContainer, IControl>.Children
        {
            get { return children; }
        }

        #endregion

    }
}
